﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Text.RegularExpressions;

namespace lcmyjobs.BLL
{
    /// <summary>
    /// 通用工具类
    /// </summary>
    public class Tools
    {
          /// <summary>
        /// 语言水平对应
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Getlevel(string str)
        {
            string result = "";

            if (str == "1")
            {
                result = "初级";
            }
            if (str == "2")
            {
                result = "熟练";
            }
            if (str == "3")
            {
                result = "精通";
            }
            return result;
        }
        /// <summary>
        /// 语言对应
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Getlanguage(string str)
        {
            string result = "";

            if (str == "1")
            {
                result = "不限";
            }
            if (str == "2")
            {
                result = "英语";
            }
            if (str == "3")
            {
                result = "日语";
            }
            if (str == "4")
            {
                result = "法语";
            }
            if (str == "5")
            {
                result = "德语";
            }
            if (str == "6")
            {
                result = "阿拉伯语";
            }
            if (str == "7")
            {
                result = "西班牙语";
            }
            if (str == "8")
            {
                result = "俄语";
            }
            if (str == "9")
            {
                result = "韩语";
            }
            if (str == "10")
            {
                result = "葡萄牙语";
            }
            if (str == "11")
            {
                result = "意大利语";
            }
            if (str == "12")
            {
                result = "波斯语";
            }
            if (str == "13")
            {
                result = "土耳其语";
            }
            if (str == "14")
            {
                result = "希腊语";
            }
            if (str == "15")
            {
                result = "瑞典语";
            }
            if (str == "16")
            {
                result = "印地语";
            }
            if (str == "17")
            {
                result = "其他";
            }

            return result;
        }

        /// <summary>
        /// 工作经验对应
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetWorkxp(string str)
        {
            string result = "";

            if (str == "1")
            {
                result = "不限";
            }
            if (str == "2")
            {
                result = "3个月以下";
            }
            if (str == "3")
            {
                result = "3个月以上";
            }
            if (str == "4")
            {
                result = "半年以上";
            }
            if (str == "5")
            {
                result = "一年以上";
            }
            if (str == "6")
            {
                result = "两年以上";
            }
            if (str == "7")
            {
                result = "三年以上";
            }
            if (str == "8")
            {
                result = "五年以上";
            }
            if (str == "9")
            {
                result = "八年以上";
            }

            return result;
        }

        /// <summary>
        /// 学历对应
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetEdu(string str)
        {
            string result = "";

            if (str == "1")
            {
                result = "不限";
            }
            if (str == "2")
            {
                result = "初中";
            }
            if (str == "3")
            {
                result = "高中";
            }
            if (str == "4")
            {
                result = "中专/中技";
            }
            if (str == "5")
            {
                result = "大专";
            }
            if (str == "6")
            {
                result = "本科";
            }
            if (str == "7")
            {
                result = "硕士";
            }
            if (str == "8")
            {
                result = "博士";
            }

            return result;
        }
        /// <summary>
        /// 薪水对应
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static  string GetSalary(string str)
        {
            string result = "";

            if (str == "0")
            {
                result = "面议";
            }
            if (str == "1")
            {
                result = "1499元以下";
            }
            if (str == "2")
            {
                result = "1500-1999元";
            }
            if (str == "3")
            {
                result = "2000-2999元";
            }
            if (str == "4")
            {
                result = "3000-4999元";
            }
            if (str == "5")
            {
                result = "5000元以上";
            }

            return result;
        }
        /// <summary>
        /// 招聘类型对应
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetWorkerclass(string str)
        {
            string result = "";
            if (str == "0")
            {
                result = "不限";
            }
            if (str == "1")
            {
                result = "往届生";
            }
            if (str == "2")
            {
                result = "在校生&bull;应届生";
            }


            return result;
        }
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetStrSub(string str, int length)
        {
            if (str.Length > length)
            {
                return str.Substring(0, length) + "...";
            }
            else
            {
                return str;
            }
        }
        /// <summary>
        /// 去除HTML标签
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string RemoveHtml(string str, int size)
        {
            Regex re = new Regex(@"<[^>]+>|</[^>]+>",
                RegexOptions.IgnoreCase |
                RegexOptions.Multiline |
                RegexOptions.IgnorePatternWhitespace |
                RegexOptions.Singleline);

            string s = re.Replace(str, "").Trim();
            return getStr(s, size);
            
        }
        public static  string cutString(string strInput, int intLen)
        {
            strInput = strInput.Trim();
            byte[] myByte = System.Text.Encoding.Default.GetBytes(strInput);
            if (myByte.Length > intLen)
            {
                //截取操作
                string resultStr = "";
                for (int i = 0; i < strInput.Length; i++)
                {
                    byte[] tempByte = System.Text.Encoding.Default.GetBytes(resultStr);
                    if (tempByte.Length < intLen - 4)
                    {
                        resultStr += strInput.Substring(i, 1);
                    }
                    else
                    {
                        break;
                    }
                }
                return resultStr;
            }
            else
            {
                return strInput;
            }
        } 
        public static string getStr(string s, int l)
        {
            string temp = s;
            if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
            {
                return temp;
            }
            for (int i = temp.Length; i >= 0; i--)
            {
                temp = temp.Substring(0, i);
                if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
                {
                    return temp;
                }
            }
            return "";
        }
        /// <summary>
        /// 发送EMail
        /// </summary>
        /// <param name="strSendEmailAdd">发送人邮箱地址</param>
        /// <param name="strSendEmailPwd">发送人邮箱密码</param>
        /// <param name="strReceEmailAdd">收件人邮箱地址</param>
        /// <param name="title">邮件标题</param>
        /// <param name="content">邮件内容</param>
        ///  <param name="mailhost">用来发送电子邮件的 SMTP 主机服务器</param>
        //private static void sendEmail(string strSendEmailAdd, string strSendEmailPwd, string strReceEmailAdd,string title,string content,string mailhost)
        //{
        //    MailMessage message = new MailMessage(strSendEmailAdd, strReceEmailAdd,title,content);
        //    //message.BodyEncoding = System.Text.Encoding.UTF8;
        //    message.IsBodyHtml = true;
        //    SmtpClient sc = new SmtpClient(mailhost);//用来发送电子邮件的 SMTP 主机服务器
        //    sc.UseDefaultCredentials = false;
        //    sc.Credentials = new NetworkCredential(strSendEmailAdd, strSendEmailPwd);
        //    sc.DeliveryMethod = SmtpDeliveryMethod.Network;
            
        //    sc.Send(message);
        //}

        /// <summary>
        /// Gridview列的合并（普通列，不包含模板列）
        /// 注意：1.GridView在绑定的时候进行分组和排序，才能让相同的行放在一起
        ///       2.方法应用的时机，应该在Gridview的DataBound事件中使用
        ///       方法调用的时机，应该是在GridView的DataBound事件中进行方法的调用。为什么要这样呢？答案很简单，我们经常会分页，这样是为了保证你分页之后，也就是页面跳转的也一页时合并仍然有效。
        /// </summary>
        /// <param name="gv">需要合并的GridView对象</param>
        /// <param name="columnIndex">所要合并列的索引</param>
        public static void UnitCell(GridView gv, int columnIndex)
        {
            int i = 0;                  //当前行数
            string lastType = string.Empty;        //当前判断是否合并行对应列的值
            int lastCell = 0;           //判断最后一个相同值的行的索引
            if (gv.Rows.Count > 0)
            {
                lastType = gv.Rows[0].Cells[columnIndex].Text.ToString();
                gv.Rows[0].Cells[columnIndex].RowSpan = 1;
                lastCell = 0;
            }
            for (i = 1; i < gv.Rows.Count; i++)
            {
                if (gv.Rows[i].Cells[columnIndex].Text == lastType)
                {
                    gv.Rows[i].Cells[columnIndex].Visible = false;
                    gv.Rows[lastCell].Cells[columnIndex].RowSpan++;
                }
                else
                {
                    lastType = gv.Rows[i].Cells[columnIndex].Text.ToString();
                    lastCell = i;
                    gv.Rows[i].Cells[columnIndex].RowSpan = 1;
                }
            }
        }

        /// <summary>
        /// Gridview列的合并（模板列）
        /// </summary>
        /// <param name="gv">需要合并的GridView对象</param>
        /// <param name="columnIndex">所要合并列的索引</param>
        /// <param name="lblName">模板列对象的ID</param>
        public static void UnitCell(GridView gv, int columnIndex, string lblName)
        {
            int i = 0;                  //当前行数
            string lastType = string.Empty;        //当前判断是否合并行对应列的值
            int lastCell = 0;           //判断最后一个相同值的行的索引
            if (gv.Rows.Count > 0)
            {
                lastType = (gv.Rows[0].Cells[columnIndex].FindControl(lblName) as Label).Text;
                gv.Rows[0].Cells[columnIndex].RowSpan = 1;
                lastCell = 0;
            }
            for (i = 1; i < gv.Rows.Count; i++)
            {
                if ((gv.Rows[i].Cells[columnIndex].FindControl(lblName) as Label).Text == lastType)
                {
                    gv.Rows[i].Cells[columnIndex].Visible = false;
                    gv.Rows[lastCell].Cells[columnIndex].RowSpan++;
                }
                else
                {
                    lastType = (gv.Rows[i].Cells[columnIndex].FindControl(lblName) as Label).Text.ToString();
                    lastCell = i;
                    gv.Rows[i].Cells[columnIndex].RowSpan = 1;
                }
            }
        }

        public static string GetValidity(string str)
        {
            string temp = "";
            if (str == "")
            {
                return "";
            }
            int val = Convert.ToInt32(str);
            if (val < 0)
            {
                temp = "过期";
            }
            else
            {
                temp = str;
            }
            return temp;
        }

        public static string GetPay(string str)
        {
            string temp = "";
            if (str == "")
            {
                return "面议";
            }



            if (str == "0")
            {
                temp = "面议";
            }
            else
            {
                temp = str;
            }
            return temp;
        }

        public static string GetJobFristIcon(string rid)
        {
            string url = "../Images/empty.gif";
            bool isfrist = false;
            isfrist = new lcmyjobs.BLL.enterprise_releasejob().isjob(Convert.ToInt32(rid), "isfrist", "1");
            if (isfrist == true)
            {
                url = "../Images/ji_03.gif";
            }
            return url;
        }

        public static string selectdisplay(string isdisplay)
        {
            string res = "显示";
            if (isdisplay == "0" || isdisplay == "False")
            {
                res = "显示";
            }
            else
            {
                res = "隐藏";
            }

            return res;
        }

        public static string GetJobAid(string aid)
        {
            string res = "L";
            if (aid == "")
            {
                aid = "0";
            }
            res += aid.PadLeft(6, '0');
            return res;
        }

        public static string GetBH(string str)
        {
            return "L" + str.ToString().PadLeft(6, '0');
        }
    }
}
